<template>
    <div>
        <h1>理财项目添加</h1>
        <table class="table table-bordered" >
            <tr>
                <td>理财名称</td>
                <td><input type="text" placeholder="请输入理财名称" v-model="F_Name"></td>
            </tr>
            <tr>
                <td>年收益</td>
                <td><input type="text" placeholder="请输入年收益" v-model="F_Apy"></td>
            </tr>
            <tr>
                <td>最低理财金额</td>
                <td><input type="text" placeholder="请输入最低理财金额" v-model="F_MinMoney"></td>
            </tr>
            <tr>
                <td>理财人数</td>
                <td><input type="text" placeholder="请输入理财人数" v-model="F_Number"></td>
            </tr>
            <tr>
                <td>利息</td>
                <td>
                    <select v-model="I_Id">
                        <option value="0">请选择</option>
                        <option v-for="item in info" :key="item.I_Id" :value="item.I_Id">{{item.I_Year}}年利息{{item.I_Interest}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>风险等级</td>
                <td>
                    <select v-model="F_Level">
                        <option value="-1">请选择</option>
                        <option value="0">低风险</option>
                        <option value="1">中风险</option>
                        <option value="2">高风险</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <button class="btn btn-primary" @click="add()">添加</button>
                </td>
            </tr>

        </table>
    </div>
</template>
<script setup lang="ts">
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const F_Name = ref('');
const F_Apy = ref('');
const F_MinMoney = ref('');
const F_Number = ref('');
const I_Id = ref(0);
const F_Level = ref(-1);

const info :any = ref([]);

onMounted(()=>{
    getInfo();
})

const getInfo =  ()=>{
    axios({
        method:'get',
        url:'http://localhost:57969/api/Finan/BindFinanInfo',
    })
    .then(res=>{
        info.value = res.data;
    })
    .catch(err=>{
        console.log(err);
    })  
    
}

const add = ()=>{
    if(F_Name.value=='')
    {
        alert('理财名称不能为空');
        return;
    }
    if(F_Apy.value=='')
    {
        alert('年收益不能为空');
        return;
    }
    if(F_MinMoney.value=='')
    {
        alert('最低理财金额不能为空');
        return;
    }
    if(F_Number.value=='')
    {
        alert('理财人数不能为空');
        return;
    }  
 
    axios({

        method:'post',
        url:'http://localhost:57969/api/Finan/AddFinanInfo',
        data:{
            F_Name:F_Name.value,
            F_Apy:F_Apy.value,
            F_MinMoney:F_MinMoney.value,
            F_Number:F_Number.value,
            I_Id:I_Id.value,
            F_Level:F_Level.value
        }
    })
    .then(res=>{
        if(res.data>0)
        {
            alert('添加成功');
            router.push('/FinanShow');
        }
        else if(res.data==-1)
        {
            alert('理财名称重复');
            return;
        }
        else
        {
            alert('添加失败');
        }

    })

    .catch(err=>{
        console.log(err);
    })
}
</script>